/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.shift.dao;

import bpmlab.shift.dao.service.GenericDAO;
import bpmlab.shift.dao.service.LocalizacaoDAO;
import bpmlab.shift.entity.Etapa;
import bpmlab.shift.entity.Localizacao;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Mikael Lima
 */
public class LocalizacaoDAOJpa implements LocalizacaoDAO {

    private final GenericDAO<Localizacao> dao = DAOFactory.criarGenericDAO(Localizacao.class);

    @Override
    public void salvar(Localizacao o) throws DAOException {
        dao.salvar(o);
    }

    @Override
    public void excluir(Localizacao o) throws DAOException {
        dao.excluir(o);
    }

    @Override
    public Localizacao obter(Object id) {
        return dao.obter(id);
    }

    @Override
    public List<Localizacao> obterTodos() {
        return dao.obterTodos();
    }

    @Override
    public List<Localizacao> obterPorEtapa(Etapa etapa) {
        EntityManager manager = JpaUtil.getEntityManager();
        String jpql = "select l from Localizacao l where :etapa member of l.etapas";
        return manager.createQuery(jpql)
                .setParameter("etapa", etapa)
                .getResultList();
    }

    @Override
    public List<Localizacao> obterLocalizacoesEEtapas() {
        EntityManager manager = JpaUtil.getEntityManager();
        String jpql = "select l from Localizacao l left join fetch l.etapas";
        return manager.createQuery(jpql, Localizacao.class).getResultList();
    }
    
}
